home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-09 | 5.7 KB | 281 lines | [TEXT/MPS ] |
- --
- -- General purpose string manipulation library
- --
- -- © 1994, 1995 Art of Memory Ltd.
- -- All Rights Reserved
- --
- -- This file is supplied as part of the TextPak demonstration. You are only
- -- entitled to use this software to understand how the demonstration works.
- -- You may not duplicate, modify, reproduce or distribute this software in
- -- any form. If you wish to purchase a licence to use this software, please
- -- contact Art of Memory Ltd.
- --
- object StringLibrary is ANY
-
- has
- showMessages;
-
- setMessages(onOrOff)
- do
- self.showMessages := onOrOff;
- end;
-
-
- substr(original, first, len)
- original is? STRING;
- first is? INTEGER;
- len is? INTEGER;
-
- --external "substring";
-
- use
- endSize;
-
- do
-
- if len=0 then
- result := void;
- else if len=1 then
- result := original @ first;
- else
- from
- result := "";
- endSize := first+len;
- while (first < endSize) loop
- result := result + (original @ first);
- step
- first := first + 1;
- end;
- end;
- end;
-
-
- -- Find the last occurence of the char in original
- -- Return the position or 0 if it does not occur
- FindLastChar(charToFind, original)
- charToFind is? CHARACTER;
- original is? STRING;
-
- use
- loopCount;
-
- do
- from
- result := 0;
- loopCount := #original;
- until (result>0 or loopCount=0) loop
- if (original @ loopCount)=charToFind then
- result := loopCount;
- end;
- step
- loopCount := loopCount - 1;
- end;
- end;
-
-
- -- Puts preString in front of postString. This is usually pretty
- -- straightforward: just using binary.+() to acheive this. But we
- -- are allowing preString to by a CHARACTER, in which case we need
- -- to increase the size of postString by one and add the preString
- -- as the first item in the new string...
- PrependString(preString, postString)
- preString is? CHARACTER or preString is? STRING;
- postString is? CHARACTER or postString is? STRING;
-
- do
-
- if self.showMessages<>void then
- --APPLICATION.Beep();
- --DanDebug.ShowAlert("PrependString: <" + preString + "> before <" + postString + ">");
- end;
-
- if preString is? CHARACTER then
- result := postString;
- result.Resize(#result + 1);
- result.Munger(1,2,#result - 1);
- result.PutAt(preString,1);
- else if postString is? CHARACTER then
- result := preString;
- result.Resize(#result + 1);
- result.PutAt(postString, #result);
- else
- if self.showMessages<>void then
- --APPLICATION.Beep();
- --DanDebug.ShowAlert("Going to put <" + preString + "> before <" + postString + ">");
- end;
- result := preString + postString;
- if self.showMessages<>void then
- --DanDebug.ShowAlert("Result is now <" + result + ">");
- end;
- end;
-
- end;
-
- LowerCase(theString)
- theString is? STRING;
- use
- loopCount;
- stringSize;
- nextChar;
-
- do
-
- result := "";
- from
- StringSize := #theString;
- loopCount := 1;
- until loopCount>stringSize loop
- nextChar := theString @ loopCount;
- if nextChar<>'\$27' and nextChar<>'’' then
- result := result + nextChar.lower();
- end;
- step
- loopCount := loopCount + 1;
- end;
-
- end;
-
- externalStringToList(inString, separator)
- inString is? STRING;
- separator is? CHARACTER;
-
- external "StringToList";
-
-
- StringToList(inString, separator)
- inString is? STRING or inString is? NONE;
- separator is? CHARACTER;
-
- do
- if inString=void then
- result := void;
- else
- if #inString=0 then
- result := void;
- else
- result := self.externalStringToList(inString, separator);
- end;
- end;
- end;
-
-
-
- -- Clean up a field value, removing tabs and other nasties
- removeSpaces(fieldValue)
- fieldValue is? STRING or fieldValue is? CHARACTER;
-
- use
- loopCount;
- nextChar;
-
- do
- if fieldValue is? CHARACTER then
- if fieldValue=' ' then
- result := "";
- else
- result := fieldValue;
- end;
- else
- result := "";
- from
- loopCount := 1;
- until (loopCount>#fieldValue) loop
- nextChar := fieldValue @ loopCount;
- if (nextChar<>' ') then
- result := result+nextChar;
- end;
- step
- loopCount := loopCount + 1;
- end;
- end;
- end;
-
-
- RemoveTrailingChars(inString, trailingChar)
- inString is? STRING;
- trailingChar is? CHARACTER;
-
- use
- loopCount;
- secondLoop;
-
- do
-
- if (inString @ (#inString))=trailingChar then
- from
- loopCount := (#inString) - 1;
- until (inString @ loopCount)<>trailingChar or loopCount=1 loop
- step
- loopCount := loopCount - 1;
- end;
-
- result := "";
- from
- secondLoop := 1;
- until secondLoop>loopCount loop
- result := result + (inString @ secondLoop);
- step
- secondLoop := secondLoop + 1;
- end;
- else
- result := inString;
- end;
-
- end;
-
-
- SeparateString(inString, separator)
- inString is? STRING;
- separator is? STRING;
-
- use
- sepPos;
-
- do
- result := new COLLECTION;
-
- if #inString<=1 then
- result.Append("null");
- else
-
- sepPos := inString.GetIndexOf(separator, 1);
- if (sepPos=0) then
- result.Append(inString);
- else
- result.Append(self.substr(inString,1, (sepPos - 1)));
- result.Append(self.substr(inString,sepPos + 1,(#instring) - sepPos));
- end;
- end;
- end;
-
- -- Convert a string, character or void (taken as ==0) to its integer
- -- value. Doesn't check first to see if the original string contains
- -- a valid integer though, so beware!
- StringToInt(theString)
- theString is? STRING or theString is? CHARACTER or theString is? NONE;
- do
- if theString is? NONE then
- result := 0;
- else
- if theString is? CHARACTER then
- result := (theString.ToString()).ToInteger();
- else
- if #theString=0 then
- result := 0;
- else
- result := theString.ToInteger();
- end;
- end;
- end;
- end;
-
- -- Needed to overcome the lack of deepEqual() for STRINGs
- StringEqual(string1, string2)
- string1 is? STRING;
- string2 is? STRING;
-
- external "StringEqual";
- end;
-
-
-